(function ($app) {
    $app.module('gtPartyApp').controller('reportDataCtrl', function ($scope, $http, $modal, $ocLazyLoad, $timeout, $alert, $loading, $state, AuthUser, $bsfiles, $partySelect, $stateParams, $bsRouterState) {
        $state.params = $bsRouterState.$params($scope);
        $scope.isShow = false;
        $scope.moreTxt = "更多";
        $scope.pageId = "checkNameList";

        //定义数据集合
        $scope.dataList = []; //列表数据
        $scope.yearList = [];
        $scope.quarterList = [];

        //初始化查询参数
        $scope.selectparams = {
            isunfold: false,//组织结构树展开收缩
            'pageindex': 1,
            'pagesize': 10,
            'selectdzzdm': '',
            selectdzzmc: '',
            defaultselected: $state.params.dzzdm,
            parentcode: AuthUser.getUser().ParentDpCode,
            parentname: AuthUser.getUser().ParentDpName,
            szdzzdm: AuthUser.getUser().DPCode,
            szdzzmc: AuthUser.getUser().DPName,
            dzzdm: '',
            dzzmc: '',
            recordStatus: null,
            year: null,
            userid: AuthUser.getUser().Id,
            onlyshow_dw: $stateParams.type == 1 ? 1 : 0,
            reportUserName: '',
            partyCode: '',
            partyName: ''
        };

        $scope.pageInfo = {ptotal: 0};
        //监视页数变化
        $scope.$watch("selectparams.pageindex", function (newVal, oldVal) {
            if ($scope.pageInfo.ptotal > 0) {
                $scope.loadData();
            }
        });
        //监视党组织选中变化
        $scope.$watch("selectparams.selectdzzdm", function (newVal, oldVal) {
            if (newVal != '' && newVal != null) {

                if ($scope.selectparams.defaultselected != "" && $scope.selectparams.defaultselected != null &&
                    $scope.selectparams.defaultselected != $scope.selectparams.selectdzzdm)
                    return;
                $scope.selectparams.defaultselected = "";
                $scope.selectparams.dzzdm = $scope.selectparams.selectdzzdm;
                $scope.loadData();
            }
        });

        //查询
        $scope.search = function () {
            $scope.loadData();
        };
        $scope.getYearsList = function () {
            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {dicTypeKey: 'years'}
            }).then(function (result) {
                $scope.yearList = result.data;
            }, function (resp) {

            });
        };
        $scope.getYearsList();

        //加载列表数据
        $scope.loadData = function () {
            $http
            ({
                method: 'get', url: '../../api/reportData/getReportdataList', params: $scope.selectparams
            }).then(function (result) {
                $scope.dataList = result.data.item.list;
                $scope.pageInfo.ptotal = result.data.item.total;
            });
        };

        var editCtrl = function ($scope, $http, $partySelect) {
            $scope.params = editModal.params;
            $scope.dataModel = editModal.data;
            $scope.userlist = [];

            $scope.selectUserlist = {
                url: '../../api/partyUser/getPartyUserList',
                title: '人员选择',
                isMulti: true,
                selectData: {xm: '姓名', szdzb: '所在组织架构'},
                columns: {XM: '姓名', XBMC: '性别', JG: '籍贯', DZZMC: '所在组织架构'},
                selectLabelKey: 'XM',
                selectValuekey: 'RYJBXXBS',
                selectedValueData: '',
                selectedLabeData: '',
                selectedJData: [],
                selectParams: {
                    ryzt: '2,3',
                    dzzdm: AuthUser.getUser().DataDzzdm
                },
                onSelect: function (selectIds, selecteds) {
                    $scope.userlist = selecteds;
                }
            };

            //保存
            $scope.save = function (isflag) {
                if (isflag) {
                    $loading.show();
                    $http({
                        method: "post",
                        url: "../../api/reportData/saveReportData",
                        data: {
                            year: $scope.dataModel.year,
                            userlist: $scope.userlist
                        }
                    }).then(function (result) {
                        $loading.hide();
                        if (result.data.success) {
                            $scope.showMsg('成功', result.data.msg);
                            //重新刷新列表
                            $scope.loadData();
                            $scope.$hide();
                        } else {
                            $scope.showMsg('失败', result.data.msg);
                        }
                    }, function (resp) {
                        $loading.hide();
                        $scope.showMsg('错误', '服务器错误');
                    });
                }
            };
        };
        editCtrl.$inject = ['$scope', '$http', '$partySelect'];
        //定义模态框
        var editModal = $modal({
            resolve: {
                load: ['$ocLazyLoad', function ($ocLazyLoad) {
                }]
            },
            scope: $scope,
            controller: editCtrl,
            templateUrl: '../main/party/reportData/edit.html',
            show: false,
            animation: 'am-fade-and-slide-top'
        });

        //新增编辑
        $scope.generate = function () {
            editModal.params = {
                title: '生成述职名单'
            };
            editModal.data = {};
            editModal.$promise.then(editModal.show);
        };

        $scope.uploadFile = function (reportId) {
            $bsfiles.addFile({
                selectdata: {FileRefID: reportId, pageindex: 1, pagesize: 10, ptotal: 0},
                readonly: false,
                filetype: 1,
                typeparams: {
                    types: ['doc', 'docx', 'xls', 'xlsx', 'pdf'],
                    errormsg: '不允许上传的文件格式'
                }
            }).then(function (fid) {
                $http({
                    method: 'get',
                    url: '../../api/reportData/changeReportDataStatus',
                    params: {reportId: reportId}
                }).then(function (result) {
                    $scope.loadData();
                }, function (resp) {
                });
            });
        }
        $scope.export = function () {
            if ($scope.selectparams.year == '' || $scope.selectparams.year == null) {
                $scope.showMsg("消息", '请选择年度');
                return;
            }
            var params = "?1=1";
            for (var p in $scope.selectparams) {
                if ($scope.selectparams[p] !== '' && $scope.selectparams[p] !== null)
                    params += ("&" + p + "=" + encodeURI($scope.selectparams[p]));
            }
            window.open('../../api/reportData/downReportDataZip' + params);
        };

        $scope.showMsg = function (title, content) {
            $alert({
                title: title + '：',
                content: content,
                placement: 'top',
                type: 'info',
                show: true,
                duration: 3
            });

        };
        $scope.isShowClick = function () {
            $scope.isShow = !$scope.isShow;
            if ($scope.moreTxt == "更多") {
                $scope.moreTxt = "收起";
            } else if ($scope.moreTxt == "收起") {
                $scope.moreTxt = "更多";
            }
        };
        $scope.setisunfold = function () {
            $scope.selectparams.isunfold = !$scope.selectparams.isunfold;
            $scope.$emit('menudatas.toggle', !$scope.selectparams.isunfold);
        };
        $scope.reset = function () {
            $scope.selectparams.year = null;
            $scope.selectparams.selectdzzmc = '';
            $scope.selectparams.reportUserName = '';
        }
    });
})(angular);
